﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace HossMvp.Core
{
    public class ModelManager : AbstractModelManager
    {
        public ModelManager()
        {
            this.Models = new List<AbstractModel>();
        }

        public ModelManager(AbstractPresenter presenter)
        {
            this.Models = presenter.ModelManager.Models;
        }

        public override AbstractModel Create<T>(params object[] args)
        {
            var argsAug = new List<object>(args);
            argsAug.Insert(0, this);

            var model = (AbstractModel)Activator.CreateInstance(typeof(T), argsAug.ToArray());
            Debug.Assert(model.Contents != null, "model.Contents != null");
            Debug.Assert(model.Manager == this, "model.Manager == this");

            // add to the list of models
            ((List<AbstractModel>)this.Models).Add(model);

            return model;
        }

        public override void Remove(AbstractModel model)
        {
            ((List<AbstractModel>)this.Models).Remove(model);
        }

        public override IEnumerable<AbstractModel> Models { get; protected set; }

        public override void Clear()
        {
            ((List<AbstractModel>)this.Models).Clear();
        }
    }
}